package com.fl.iocaop.factory;

import com.fl.iocaop.domain.Bean_List;
import com.fl.iocaop.domain.Dept;
import com.fl.iocaop.domain.Emp;
import com.fl.iocaop.domain.User;
import com.fl.iocaop.service.UserService;
import org.apache.naming.factory.BeanFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 工厂模式！
 */
public class UserFactory {
    public static User getUser(){
        return new User();
    }
    //bean反射----利用ioc原理
    public static void TestBean(){
        ApplicationContext factory = new ClassPathXmlApplicationContext("bean1.xml");
        User user = factory.getBean("user",User.class);
        System.out.println(user);
        System.out.println(user.add());
    }
    //测试构造方法与set方法注入
    public static Dept TestBean1(){
        ApplicationContext factory = new ClassPathXmlApplicationContext("bean1.xml");
        Dept dept = factory.getBean("dept", Dept.class);
        System.out.println(dept);
        return dept;
    }
    //测试接口注入
    public static void TestBean2(){
        ApplicationContext factory = new ClassPathXmlApplicationContext("bean2.xml");
        UserService service = factory.getBean("UserService", UserService.class);
        service.SayUser();
    }
    //测试对象注入-内部注入
    public static void TestBean3(){
        ApplicationContext factory = new ClassPathXmlApplicationContext("bean3.xml");
        Emp emp = factory.getBean("emp", Emp.class);
        System.out.println(emp.toString());
    }
    //测试集合注入与外联注入
    public static Bean_List TestBean4(){
        ApplicationContext factory = new ClassPathXmlApplicationContext("bean4.xml");
        Bean_List bean = factory.getBean("bean", Bean_List.class);
        return bean;
    }
    //测试自定义集合注入
    public static void TestBean5(){
        ApplicationContext factory = new ClassPathXmlApplicationContext("bean5.xml");
        Bean_List bean = factory.getBean("beanlist", Bean_List.class);
        System.out.println(bean.toString());
    }

    //测试自定义集合注入
    //工厂bean模式 可自定义返回bean的对象类型
    @Test
    public static void TestBean7(){
//        UserFactory.TestBean2();
        ApplicationContext factory = new ClassPathXmlApplicationContext("bean6.xml");
        Dept dept = factory.getBean("factorybean", Dept.class);
        System.out.println(dept.toString());
    }
}
